<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>费用协议编辑</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        function formatDate(date) {
            var datetime = date.getFullYear()
                           + "-"// "年"
                           + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
                           + (date.getMonth() + 1))
                           + "-"// "月"
                           + (date.getDate() < 10 ? "0" + date.getDate() : date
                    .getDate());
            return datetime;
        }

        var today = formatDate(new Date());
        var eftTimeBegin = today;
        if ("${platformFee.eftTimeBegin}") {
            eftTimeBegin = formatDate(new Date(${platformFee.eftTimeBegin.getTime()}));
        }
        var eftTimeEnd = today;
        if ("${platformFee.eftTimeEnd}") {
            eftTimeEnd = formatDate(new Date(${platformFee.eftTimeEnd.getTime()}));
        }
        var status = "草稿";
        if ("0" == "${platformFee.status}") {
            status = "草稿";
        } else if ("1" == "${platformFee.status}") {
            status = "待生效";
        } else if ("2" == "${platformFee.status}") {
            status = "生效";
        } else if ("3" == "${platformFee.status}") {
            status = "结束";
        }
        var formSetting = {
            title: "费用协议编辑",
            module: 'save',
            columns: [{name: "id", value: "${platformFee.id}", hidden: true},
                {
                    name: "customerId",
                    label: "客户名称",
                    value: "${platformFee.customerName}",
                    type: 'dropdown',
                    dropdownType: "ajax",
                    keyWordName: 'key',
                    url: "/order-dropdown/platformFeeCustomer",
                    minLength: 0,
                    success: function (data) {
                        data = data.results;
                        var array = [];
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "companyType",
                    label: "公司类型",
                    value: "${platformFee.companyTypeChn}",
                    type: "dropdown",
                    dropdownType: "ajax",
                    keyWordName: 'key',
                    url: "/order-dropdown/getCompanyType",
                    minLength: 0,
                    success: function (data) {
                        data = data.results;
                        var array = [];
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "statusChn",
                    value: status,
                    label: "状态",
                    readonly: true,
                }, {
                    name: "eftTimeBegin",
                    label: "生效时间起",
                    value: eftTimeBegin,
                    type: 'date',
                    validate: 'validate[required]'
                }, {
                    name: "eftTimeEnd",
                    label: "生效时间止",
                    value: eftTimeEnd,
                    type: 'date',
                    validate: 'validate[required]'
                }, {
                    name: "agentSelf",
                    label: "是否委托自己",
                    value: "${platformFee.agentSelfChn}",
                    type: 'dropdown',
                    dropdownType: "ajax",
                    url: "/order-dropdown/getAgentSelf",
                    minLength: 0,
                    success: function (data) {
                        data = data.results;
                        var array = [];
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "feeName",
                    label: "费用名称",
                    value: "${platformFee.feeName}",
                    validate: 'validate[required]'
                }, {
                    name: "chargeMode",
                    label: "费用规则",
                    validate: 'validate[required]',
                    value: "${platformFee.chargeModeChn}",
                    type: "dropdown",
                    dropdownType: "ajax",
                    url: "/order-dropdown/getChargeMode",
                    minLength: 0,
                    success: function (data) {
                        data = data.results;
                        var array = [];
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "feeAmount",
                    label: "单价",
                    value: "${platformFee.feeAmount}",
                    validate: 'validate[required]'
                }, {
                    name: "supplierId",
                    label: "报关行",
                    value: "${platformFee.supplierName}",
                    type: "dropdown",
                    dropdownType: "ajax",
                    keyWordName: 'key',
                    url: "/order-dropdown/supCompanyByKey",
                    minLength: 0,
                    success: function (data) {
                        data = data.results;
                        var array = [];
                        $.each(data, function (i, n) {
                            array.push([n["name"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "status",
                    value: "${platformFee.status}" ? "${platformFee.status}" : "0",
                    hidden: true
                }
            ],
            buttons: {
                custom: [
                    {
                        id: "submitBtn",
                        value: "提交",
                        click: function () {
                            jw.confirm("协议提交后进入待生效状态，将不能再修改，到生效时间时自动生效，请确认当前操作",
                                       function () {
                                           $("input[name='status']").val("1");
                                           $("#saveButton").click();
                                       })
                        }
                    }, {
                        id: "copyBtn",
                        value: "复制",
                        click: function () {
                            $.ajax({
                                       url: "copyPlatformFee",
                                       type: "post",
                                       data: {"id": "${platformFee.id}"},
                                       cache: false,
                                       success: function (data) {
                                           if (data.result) {
                                               jw.confirm("进入复制的协议页面?", function () {
                                                   var id = data.data.id;
                                                   location.href = "editPage?id=" + id;
                                               })
                                           } else {
                                               jw.alert(data.msg);
                                           }
                                       }
                                   });
                        }
                    }, {
                        id: "endBtn",
                        value: "结束",
                        click: function () {
                            jw.confirm("确定要结束协议？", function () {
                                $("input[name='status']").val("3");
                                $("#saveButton").click();
                            })
                        }
                    }, {
                        id: "btn5",
                        value: "返回",
                        click: function () {
                            location.href = "listPage";
                        }
                    }
                ]
            }, form: {
                'url': 'saveOrUpdate',
                'ajaxForm': {
                    success: function (data) {
                        layer.alert(data.msg, {
                            yes: function (index) {
                                layer.close(index);
                                if (data.result) {
                                    var id = data.data.id;
                                    location.href = "editPage?id=" + id;
                                }
                            }
                        })
                    }
                }
            }
        };
        $(document).ready(function () {
            if ("${platformFee.status}" && "${platformFee.status}" != "0") {
                for (var i = 0; i < formSetting.columns.length; i++) {
                    formSetting.columns[i].readonly = true;
                }
            }
            $("#table").mrdForm(formSetting);
            if (!"${platformFee.id}") {//新增
                $("#copyBtn").hide();
            }
            if (!"${platformFee.status}" || "0" == "${platformFee.status}"
                || "3" == "${platformFee.status}") {
                $("#endBtn").hide();
            } else {
                $("#saveButton").hide();
            }
            if (!(!"${platformFee.status}" || "0" == "${platformFee.status}")) {
                $("#submitBtn").hide();
            }
            if ("${platformFee.customerName}") {
                $("input[name='customerId']").val("${platformFee.customerId}");
                $("input[name='control_customerId_dropdown']").val("${platformFee.customerName}");
            }
            if ("${platformFee.companyTypeChn}") {
                $("input[name='companyType']").val("${platformFee.companyType}");
                $("input[name='control_companyType_dropdown']")
                    .val("${platformFee.companyTypeChn}");
            }
            if ("${platformFee.agentSelf}") {
                $("input[name='agentSelf']").val("${platformFee.agentSelf}");
                $("input[name='control_agentSelf_dropdown']").val("${platformFee.agentSelfChn}");
            }
            if ("${platformFee.chargeMode}") {
                $("input[name='chargeMode']").val("${platformFee.chargeMode}");
                $("input[name='control_chargeMode_dropdown']").val("${platformFee.chargeModeChn}");
            }
            if ("${platformFee.supplierName}") {
                $("input[name='supplierId']").val("${platformFee.supplierId}");
                $("input[name='control_supplierId_dropdown']").val("${platformFee.supplierName}");
            }
        });
    </script>
</head>
<body>
<div id="table"></div>
</body>
</html>
